package com.seeyou.lake.ui

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.core.app.ActivityOptionsCompat
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout.OnRefreshListener
import com.chad.library.adapter4.BaseQuickAdapter
import com.chad.library.adapter4.viewholder.QuickViewHolder
import com.seeyou.lake.R
import com.seeyou.lake.base.BaseFragment
import com.seeyou.lake.base.EventBusVo
import com.seeyou.lake.bean.ArticleBean
import com.seeyou.lake.bean.UserBean
import com.seeyou.lake.databinding.FragmentNormalBinding
import com.seeyou.lake.util.GlideUtil
import com.seeyou.lake.util.SPUtils
import com.seeyou.lake.viewmodel.MainViewModel
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class NormalFragment : BaseFragment<FragmentNormalBinding, MainViewModel>() {

    private lateinit var userBean: UserBean

    override fun yourViewModel(): MainViewModel {
        return MainViewModel()

    }

    override fun yourViewBing(): FragmentNormalBinding {
        return FragmentNormalBinding.inflate(layoutInflater)
    }

    override fun init() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this)
        }
         userBean = SPUtils.getInstance().getObject(SPUtils.SP_USER) as UserBean
        mViewBing.srlNormal.setColorSchemeColors(ContextCompat.getColor(requireContext(),R.color.badge_red))
        mViewBing.srlNormal.setOnRefreshListener(OnRefreshListener {
            mViewBing.srlNormal.isRefreshing = false
            mVModel.getArticleList(userBean.uid,0)
        })
        val gridLayoutManager =object :GridLayoutManager(requireContext(),2,RecyclerView.VERTICAL,false){
            override fun canScrollHorizontally(): Boolean {
                return false
            }

            override fun canScrollVertically(): Boolean {
                return true
            }
        }

        val adapter = object : BaseQuickAdapter<ArticleBean, QuickViewHolder>() {
            override fun onCreateViewHolder(
                context: Context,
                parent: ViewGroup,
                viewType: Int
            ): QuickViewHolder {
                // 返回一个 ViewHolder
                return QuickViewHolder(R.layout.item_article, parent)
            }

            override fun onBindViewHolder(
                holder: QuickViewHolder,
                position: Int,
                item: ArticleBean?
            ) {
                val imgView = holder.getView<ImageView>(R.id.iv_item_article)
                GlideUtil.loadRoundImage(imgView.context,item?.url,8,imgView)
                holder.setText(R.id.tv_item_title,item?.title)
            }
        }
        mViewBing.rlNormal.layoutManager = gridLayoutManager
        mViewBing.rlNormal.adapter = adapter

        adapter. addOnItemChildClickListener(R.id.iv_item_article
        ) { adapter, view, position ->
            val intent = Intent(requireContext(),ArticleDetailActivityMy::class.java)
            intent.putExtra("articleBean", adapter.getItem(position)!!)
            val options = ActivityOptionsCompat.makeSceneTransitionAnimation(
                requireActivity(),
                view,
                "shareImage"
            )
            startActivity(intent, options.toBundle())
        }

        mVModel.getArticleList(userBean.uid,0)

        mVModel.normalArticleResponse.observe(this){
            adapter.submitList(it.data)
            if (it.data==null|| it.data!!.isEmpty()){
                mViewBing.rlNormal.visibility = View.GONE
                mViewBing.llEmpty.visibility = View.VISIBLE
            }else{
                mViewBing.llEmpty.visibility = View.GONE
                mViewBing.rlNormal.visibility = View.VISIBLE

            }
        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun finishEvent(event: EventBusVo?) {
        if (event == null) {
            return
        }

        if (event.type == 2) {
            mVModel.getArticleList(userBean.uid,0)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this)
        }
    }


    companion object {
        @JvmStatic
        fun newInstance() =
            NormalFragment()
    }
}